package algorithms.que13;

import edu.princeton.cs.algs4.StdRandom;

import java.util.Iterator;
import java.util.Random;

/**
 * @功能
 * @创建时间 2020/7/12
 */
public class RandomBag<Item> implements Iterable<Item>{
    private  Node first;
    public int size;
    private class Node{
        Item item;
        Node next;
    }
    public boolean isEmpty(){
        return first==null;
    }
    public void add(Item item){
        Node oldFirst =first;
        first=new Node();
        first.next=oldFirst;
        first.item=item;
        size++;
    }
    @Override
    public Iterator<Item> iterator() {
        return new ListIterator();
    }
    private class ListIterator implements Iterator<Item>{
        private Node current = first;
        @Override
        public boolean hasNext() {
            return current!=first;
        }
        public void remove(){
            Integer a = StdRandom.uniform(size);
            if(a==1&&size==1){
                current.item=null;
            }else if(a==2){

            }else
           for (int i=2;i<a;i++){
               current=current.next;
               current.next=current.next.next;
           }
        }
        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}
